package com.tanhua.admin.controller;

import cn.hutool.captcha.LineCaptcha;
import com.tanhua.admin.service.AdminService;
import com.tanhua.model.dto.LoginDto;
import com.tanhua.model.vo.AdminVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Xiao
 * @version 1.0
 * @description 系统框架接口
 * @date 2021/11/3
 */
@RestController
@RequestMapping("/system/users")
public class SystemController {
    @Autowired
    private AdminService adminService;

    /**
     * 系统框架-获取验证码图片
     * @param uuid
     * @return
     */
    @GetMapping("/verification")
    public ResponseEntity getVerification(String uuid, HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha = adminService.getVerification(uuid);
        //输出验证码图片
        lineCaptcha.write(response.getOutputStream());
        return ResponseEntity.ok(null);
    }

    /**
     * 系统框架-用户登录
     * @param loginDto  username、password、verificationCode、uuid
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody LoginDto loginDto) {
        Map<String, String> tokenMap = adminService.login(loginDto);
        return ResponseEntity.ok(tokenMap);
    }

    /**
     * 系统框架-用户基本信息
     * @return
     */
    @PostMapping("/profile")
    public ResponseEntity getProfile() {
        AdminVO adminVO = adminService.getProfile();
        return ResponseEntity.ok(adminVO);
    }

    /**
     * 系统框架-用户登出
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity logout() {
        adminService.logout();
        return ResponseEntity.ok("已退出登录！");
    }

}
